from django.conf import settings
from django.contrib import admin
from modeltranslation.admin import TranslationAdmin
from modeltranslation.utils import build_localized_fieldname
from photologue.admin import GalleryAdmin, PhotoAdmin
from photologue.models import Gallery, Photo, GalleryUpload


_DEFAULT_LANG = settings.LANGUAGES[0][0]
def _slugify_field(slug_field, prepopulate_from): 
    slug_from = build_localized_fieldname(prepopulate_from, _DEFAULT_LANG)
    return {slug_field: [slug_from]}


class TranslatatedGalleryAdmin(GalleryAdmin, TranslationAdmin):
    prepopulated_fields = _slugify_field('title_slug', 'title')

class TranslatatedPhotoAdmin(PhotoAdmin, TranslationAdmin):
    prepopulated_fields = _slugify_field('title_slug', 'title')

class GalleryUploadAdmin(admin.ModelAdmin):
    def has_change_permission(self, request, obj=None):
        return False # To remove the 'Save and continue editing' button


admin.site.unregister(Gallery)
admin.site.register(Gallery, TranslatatedGalleryAdmin)

admin.site.unregister(Photo)
admin.site.register(Photo, TranslatatedPhotoAdmin)

admin.site.unregister(GalleryUpload)
admin.site.register(GalleryUpload, GalleryUploadAdmin)
